package com.jgj.zxkj.lookandcheckjggroupinfo.record.detail;

import android.content.Context;
import android.util.SparseArray;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jgj.zxkj.lookandcheckjggroupinfo.R;
import com.jgj.zxkj.lookandcheckjggroupinfo.constants.Constants;
import com.sa.all_cui.mix_core.app.MIX;
import com.sa.all_cui.mix_ui.recycler.DataConvert;
import com.sa.all_cui.mix_ui.recycler.ItemType;
import com.sa.all_cui.mix_ui.recycler.MultipleItemEntity;

import java.util.ArrayList;

/**
 * Created by all-cui on 2017/11/2.
 */

public class CheckDetailDataConvert extends DataConvert {

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        JSONObject res = JSON.parseObject(getJsonData());
        final String success = res.getString("success");
        if (!Constants.SUCCESS_FLAG.equals(success)) {
            return null;
        }
        JSONObject data = res.getJSONObject("data");
        //申请单位
        final String applyDepartment = data.getString("sqdw");
        //联系人
        final String contacter = data.getString("lxr");
        //电话号码
        final String contactPhone = data.getString("dh");
        //道路名称
        final String routerName = data.getString("dlmc");
        //道路用途
        final String routerUsage = data.getString("yt");
        //办理人
        final String operator = data.getString("blr");
        //竣工日期
        final String finishDate = data.getString("jgrq");
        //办理日期
        final String operateDate = data.getString("blsj");

        Context context = MIX.getContext();
        MultipleItemEntity baseInfo = MultipleItemEntity.builder().
                setField(ItemType.TEXT, context.getString(R.string.detail_base_info))
                .setField(ItemType.TYPE, ItemType.TEXT).build();
        ENTITYS.add(baseInfo);
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_router_name), routerName));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_contacter), contacter));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_contact_phone), contactPhone));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_apply_department), applyDepartment));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_finish_date), finishDate));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_operate_date), operateDate));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_operator), operator));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_operate_date), operateDate));
        ENTITYS.add(appendItemEntity(context.getString(R.string.detail_router_usage), routerUsage));

        return ENTITYS;
    }

    private MultipleItemEntity appendItemEntity(String name, String value) {
        SparseArray<String> valueArray = new SparseArray<>();
        valueArray.put(0, name);
        valueArray.put(1, value);
        return MultipleItemEntity.builder().setField(ItemType.TEXT_DOUBLE, valueArray)
                .setField(ItemType.TYPE, ItemType.TEXT_DOUBLE).build();
    }
}
